package com.zwu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwu.Mapper.StudentMapper;
import com.zwu.enums.uploadEnum;
import com.zwu.exception.uploadException;
import com.zwu.pojo.Count;
import com.zwu.pojo.Result;
import com.zwu.pojo.Student;
import com.zwu.service.StudentService;
import com.zwu.util.ResultVoUtil;
import com.zwu.vo.ResultVo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Zyl
 * @Param:
 * @return:
 * @Description:
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;

    @Override
    public Student findStatus(String identify) {

        Integer status=0;
        Student student = studentMapper.findStatus(identify);
        if (student!=null){
            status=1;
            student.setStatus(status);
        }
        return student;
    }

    @Override
    public List<Student> findAll() {


        return studentMapper.findAll();
    }

    @Override
    public Result add(MultipartFile file) throws IOException {
        int result = 0;
        List<Student> studentList = new ArrayList<>();
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        InputStream ins = file.getInputStream();
        Workbook wb = null;
        if (suffix.equals("xlsx")) {
            wb = new HSSFWorkbook(ins);
        } else {
            return new Result(0,uploadEnum.NO_FILE.getMsg());
        }
        Sheet sheet = wb.getSheetAt(0);//获取工作表
        if (sheet != null) {
            for (int line = 1; line <= sheet.getLastRowNum(); line++) {//遍历工作表
               Student student = new Student();
                Row row = sheet.getRow(line);
                if (row == null) {//空行跳过
                    continue;
                }
//                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);//第0行
//                String id = row.getCell(0).getStringCellValue();
//
//                if (id==""){
//                    return new Result(0,uploadEnum.NULL_ID.getMsg());
//                }
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);//第1行
                String num = row.getCell(0).getStringCellValue();
                if (num==""){
                    return new Result(0,uploadEnum.NULL_NUM.getMsg());
                }
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);//第2行
                String name = row.getCell(1).getStringCellValue();
                if (name==""){
                    return new Result(0,uploadEnum.NULL_NAME.getMsg());
                }
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);//第3行
                String identify = row.getCell(2).getStringCellValue();
                if (identify==""){
                    return new Result(0,uploadEnum.NULL_IDENTIFY.getMsg());
                }
                row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);//第4行
                String province = row.getCell(3).getStringCellValue();
                if (province==""){
                    return new Result(0,uploadEnum.NULL_PROVINCE.getMsg());
                }
                row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);//第4行
                String dept = row.getCell(4).getStringCellValue();
                if (dept==""){
                    return new Result(0,uploadEnum.NULL_DEPT.getMsg());
                }
                student.setNum(num);
                student.setName(name);
                student.setIdentify(identify);
                student.setProvince(province);
               student.setDept(dept);
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                student.setTime(df.format(new Date()));
                studentList.add(student);
            }
            for (Student stuInfo : studentList) {

                Integer sameStudent = studentMapper.findSameStudent(stuInfo.getIdentify());
                if (sameStudent!=0){
                    return new Result(0,uploadEnum.SAME_FILE.getMsg()+":"+stuInfo.getName());
                }
                result = studentMapper.add(stuInfo);

            }


        }
        System.out.println(result);
        return new Result(1,"成功");
    }

    @Override
    public  List<Student> findStudent(String identify, String time) {
        return  studentMapper.findStudent(identify,time);
    }

    @Override
    public void deleteStudent(Integer id) {
        studentMapper.deleteStudent(id);
    }

    @Override
    public Student findStudentById(Integer id) {
        return studentMapper.findStudentById(id);
    }


    @Override
    public PageInfo<Student> findAllByPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<Student> studentList = studentMapper.findAll();
        PageInfo<Student> pageInfo = new PageInfo<>(studentList);
        return pageInfo;
    }

    @Override
    public void updateStudent(Student student) {

        studentMapper.updateStudent(student);
    }

    @Override
    public Integer findSameStudent(String identify) {
        return studentMapper.findSameStudent(identify);
    }

    @Override
    public Result addStudent(Student student) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        student.setTime(df.format(new Date()));
        if (studentMapper.findSameStudent(student.getIdentify())!=0){
            return new Result(0,uploadEnum.SAME_FILE.getMsg());
        }
        if (student.getName()==""){
            return new Result(0,"名字不能为空");
        }
        if (student.getNum()==""){
            return new Result(0,"准考证不能为空");
        }
        if (student.getIdentify()==""){
            return new Result(0,"身份证不能为空");
        }
        if (student.getProvince()==""){
            return new Result(0,"省份不能为空");
        }
        if (student.getDept()==""){
            return new Result(0,"专业不能为空");
        }
        studentMapper.addStudent(student);
        return new Result(1,"添加成功");
    }

    @Override
    public Count count() {
        Count count=new Count(studentMapper.allStudent(),studentMapper.findZheJiang(),studentMapper.noZheJiang(),studentMapper.elseProvince());
        return count;
    }

    @Override
    public Integer allStudent() {
        return studentMapper.allStudent();
    }


}
